﻿using System;
using System.Collections.Generic;
using ProjScan.Modules;
using ProjScan.ScanRule;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.RecordManager
{
	// Token: 0x02000283 RID: 643
	internal class RecordManagerFactory
	{
		// Token: 0x06001102 RID: 4354 RVA: 0x0004F8A4 File Offset: 0x0004DAA4
		public void Init()
		{
			bool flag = this.dicManager == null;
			if (flag)
			{
				this.dicManager = new Dictionary<Type, IRecordManager>();
			}
			else
			{
				this.dicManager.Clear();
			}
			this.isInit = true;
		}

		// Token: 0x06001103 RID: 4355 RVA: 0x0004F8E4 File Offset: 0x0004DAE4
		public IRecordManager GetManager<T>() where T : IRecordManager, new()
		{
			bool flag = !this.isInit;
			if (flag)
			{
				throw new Exception("Record Manager Factory has not been inited or prepared");
			}
			Type typeFromHandle = typeof(T);
			IRecordManager recordManager;
			bool flag2 = !this.dicManager.TryGetValue(typeFromHandle, out recordManager);
			if (flag2)
			{
				recordManager = Singleton<T>.Instance;
				bool flag3 = recordManager == null;
				if (flag3)
				{
					throw new Exception("Manager of this Type is not added in the RecordManagerFactory : " + ((typeFromHandle == null) ? null : typeFromHandle.ToString()));
				}
				this.dicManager.Add(typeFromHandle, recordManager);
			}
			return recordManager;
		}

		// Token: 0x06001104 RID: 4356 RVA: 0x0004F97C File Offset: 0x0004DB7C
		public void RecordRuleInit(List<RecordRule> list)
		{
			Type typeFromHandle = typeof(RecordRule);
			foreach (BaseRule baseRule in list)
			{
				bool flag = baseRule.GetType().IsSubclassOf(typeFromHandle);
				if (flag)
				{
					RecordRule recordRule = baseRule as RecordRule;
					recordRule.GetManager().AddRecordRule(recordRule);
				}
			}
		}

		// Token: 0x06001105 RID: 4357 RVA: 0x0004F9FC File Offset: 0x0004DBFC
		public void PrepareAssets()
		{
			bool flag = !this.isInit;
			if (flag)
			{
				throw new Exception("Record Manager Factory has not been inited");
			}
			bool flag2 = this.dicManager == null || this.dicManager.Count == 0;
			if (flag2)
			{
				throw new Exception("Managers is empty");
			}
			foreach (KeyValuePair<Type, IRecordManager> keyValuePair in this.dicManager)
			{
				try
				{
					Debug.Log("Prepare Assets" + keyValuePair.Value.TableTag);
					keyValuePair.Value.PrepareAssets();
				}
				catch (Exception ex)
				{
					Debug.LogError(string.Concat(new string[]
					{
						"Scan: 资源类型 ",
						keyValuePair.Value.TableTag,
						" 扫描失败\n",
						ex.Message,
						"\n",
						ex.StackTrace
					}));
				}
				ToolsHelper.GC();
			}
		}

		// Token: 0x06001106 RID: 4358 RVA: 0x0004FB28 File Offset: 0x0004DD28
		public void OutPutScanScr()
		{
			foreach (KeyValuePair<Type, IRecordManager> keyValuePair in this.dicManager)
			{
				try
				{
					keyValuePair.Value.OutPutScanScr();
				}
				catch (Exception ex)
				{
					ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, "RecordManagerFactory", string.Concat(new string[]
					{
						(ex.GetType() == null) ? null : ex.GetType().ToString(),
						"\n",
						ex.Message,
						"\n",
						ex.StackTrace
					}));
				}
			}
		}

		// Token: 0x06001107 RID: 4359 RVA: 0x0004FBFC File Offset: 0x0004DDFC
		public void Close()
		{
			foreach (KeyValuePair<Type, IRecordManager> keyValuePair in this.dicManager)
			{
				try
				{
					keyValuePair.Value.Close();
				}
				catch (Exception ex)
				{
					Debug.LogError(ex);
				}
			}
		}

		// Token: 0x040009F0 RID: 2544
		public static readonly RecordManagerFactory Instance = new RecordManagerFactory();

		// Token: 0x040009F1 RID: 2545
		private Dictionary<Type, IRecordManager> dicManager;

		// Token: 0x040009F2 RID: 2546
		private bool isInit = false;
	}
}
